<template>
    <div>
        <div class="formContainer">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm jform">
                <el-form-item label="图书编号" prop="bkId" label-width="120px">
                    <el-input v-model="ruleForm.bkId" style="width: 217px;"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">立即查询</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>


        </div>
        <el-divider>用户信息</el-divider>
        <div class="tableContainer">
            <el-table :data="readerInfo" stripe size="medium">
                <el-table-column prop="rdId" label="编号"></el-table-column>
                <el-table-column prop="rdName" label="姓名"></el-table-column>
                <el-table-column prop="rdSex" label="性别"></el-table-column>
                <el-table-column prop="rdTypeName" label="类别"></el-table-column>
                <el-table-column prop="rdDept" label="单位"></el-table-column>
                <el-table-column prop="rdPhone" label="电话"></el-table-column>
                <el-table-column prop="rdEmail" label="邮箱" width="160px"></el-table-column>
                <el-table-column prop="rdDateReg" label="办卡日期" width="160px"></el-table-column>
                <el-table-column prop="rdStatus" label="状态"></el-table-column>
                <el-table-column prop="rdBorrowQty" label="借阅数量"></el-table-column>
                <el-table-column prop="canLendQty" label="借阅上限"></el-table-column>
                <el-table-column prop="rdPwd" label="密码"></el-table-column>
                <el-table-column prop="rdAdminRoles" label="角色"></el-table-column>
            </el-table>
        </div>
        <el-divider>借书信息</el-divider>
        <div class="tableContainer">
            <el-table :data="borrowInfoList" stripe size="medium">
                <el-table-column prop="bkId" label="编号"></el-table-column>
                <el-table-column prop="bkName" label="书名"></el-table-column>
                <el-table-column prop="bkAuthor" label="作者"></el-table-column>
                <el-table-column prop="ldDateOut" label="借书时间" width="160px"></el-table-column>
                <el-table-column prop="ldPlanDateRet" label="预还时间" width="160px"></el-table-column>
                <el-table-column prop="ldDateRetAct" label="还书时间" width="160px"></el-table-column>
                <el-table-column prop="ldOverDay" label="逾期天数"></el-table-column>
                <el-table-column prop="ldOverMoney" label="逾期金额"></el-table-column>
                <el-table-column prop="ldPunishMoney" label="罚款金额"></el-table-column>
                <el-table-column prop="bkPress" label="出版社"></el-table-column>
                <el-table-column prop="bkDatePress" label="出版时间" width="160px"></el-table-column>
                <el-table-column prop="bkISBN" label="ISBN"></el-table-column>
                <el-table-column prop="bkCatalog" label="分类"></el-table-column>
                <el-table-column prop="bkLanguage" label="语言"></el-table-column>
                <el-table-column prop="bkPages" label="页数"></el-table-column>
                <el-table-column prop="bkPrice" label="价格"></el-table-column>
                <el-table-column prop="bkDateIn" label="入馆时间" width="160px"></el-table-column>
                <el-table-column prop="bkStatus" label="状态"></el-table-column>
                <el-table-column prop="bkBrief" label="书籍简介"></el-table-column>
                <el-table-column prop="operatorBorrow" label="借书操作员"></el-table-column>
                <el-table-column prop="operatorReturn" label="还书操作员"></el-table-column>
                <el-table-column
                        fixed="right"
                        label="操作"
                        width="100">
                    <template slot-scope="scope">
                        <el-button @click="handleClickDelete()" type="text" size="small">删除</el-button>
                        <el-button @click="handleClickReturn()" type="text" size="small">还书</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-divider></el-divider>




    </div>
</template>

<script>
    import {mapState} from 'vuex'
    import {returnBook} from "@/api";

    export default {
        name: "",
        data() {
            return {
                drawer: false,
                direction: 'rtl',
                show: false,
                searchParams: {},
                ruleForm: {
                    id:'',
                    bkId: '',
                },
                bookForm: {
                    name: ''
                },
                returnParams:{
                    borrowId:null,
                    operatorReturn:''
                }
                ,
                rules: {
                    bkId: [
                        {required: true, message: '请输入图书编号', trigger: 'blur'},
                        {min: 1, max: 5, message: '长度在 1到 5 个字符', trigger: 'blur'}
                    ]
                }
            };
        },
        computed: {
            ...mapState({
                readerInfo: (state) => state.Borrow.readerInfo
            }),
            ...mapState({
                borrowInfoList: (state) => state.Borrow.borrowInfoList
            }),
            ...mapState({
                loginReader: (state) => state.Reader.loginReader
            }),
        },
        activated() {
            this.$store.dispatch("clear")
        }
        ,
        methods: {

            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$store.commit("CLEARINFO")
                        this.$store.dispatch("queryBorrow", this.ruleForm)
                        this.readerInfo = this.queryResult.readerJoinType
                        this.booksInfo = this.queryResult.borrowedList
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
            ,
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            goBorrow() {
                this.$router.replace("/borrow/borrowBook/borrow")
            },
            handleClickReturn(){
                this.returnParams.borrowId=this.borrowInfoList[0].borrowId
                this.returnParams.operatorReturn=this.loginReader.username
                this.returnBookFn(this.returnParams)
            },
            async returnBookFn(params={}){
                let response = await returnBook(params);
                if (response.code==200){
                    alert("还书成功")
                    this.$store.commit("CLEARINFO")
                }else {
                    alert(response.msg)
                }
            },
        }

    }
</script>

<style scoped>
    .formContainer {
        width: 500px;
        margin: 0 auto;
    }

    .tableContainer {
        position: relative;
        right: 0px;
        left: 0px;
    }
</style>
